<?php

declare(strict_types=1);

namespace App\Admin\Actions\Image;

use App\Models\Admin\Author;
use App\Models\Admin\Image;
use Illuminate\Http\Request;
use Encore\Admin\Actions\RowAction;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;

class ImageRecommendAction extends RowAction
{
    public $name = "推荐";

    public function handle(Model $model, Request $request)
    {
        $sort = $request->post("sort", "");
        $isRecommend = $request->post("is_recommend", 2);
        $uid = $request->post("uid", "");

        $updateRow = Image::query()->where("uid", "=", $uid)->update([
            "is_recommend" => $isRecommend,
            "sort"         => $sort,
        ]);
        if ($updateRow) {
            return $this->response()->success("设置成功")->refresh();
        }
        return $this->response()->error("设置失败");
    }

    public function form()
    {
        $uid = $this->getRow()->getAttribute("uid");
        $image = Image::query()->where("uid", "=", $uid)->first(["is_recommend"]);
        $isRecommend = empty($image) ? 2 : 1;
        $this->hidden("uid", "数据id")->default($uid);
        $this->text("sort", "显示权重")->rules("required|integer")->help("值越大显示权重越高");
        $this->radio('is_recommend', '推荐状态')->default($isRecommend)->options([1 => "推荐", 2 => "取消"])->required();
    }
}
